package cn.com.ddapp.ddm.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.bean.mine.Card;
import cn.com.ddapp.ddm.ui.mine.BindBankActivity;
import cn.com.ddapp.ddm.util.TDevice;

/**
 * Created by admin on 2016/8/29.
 */
public class BankListAdapter extends BaseAdapter {
    private Context context;
    private List<Card.ListBean> list;
    private int start = -1;
    private int close = -1;
    private boolean all = true;
    int orientation;

    public BankListAdapter(Context context, List<Card.ListBean> list,int orientation) {
        this.context = context;
        this.list = list;
        this.orientation = orientation;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == list.size() - 1) {
            return 0;
        } else {
            return 1;
        }
    }

    //某一条改变背景
    public void setMySoothAlterPosit(int index) {
        this.start = index;
        notifyDataSetChanged();
    }

    //某一条恢复原状
    public void setMySoothClosePosit(int index) {
        this.close = index;
        notifyDataSetChanged();
    }

    public void setOld(boolean all) {
        this.all = all;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        ViewHolder1 holder1 = null;
        int type = getItemViewType(i);
        if (convertView == null) {
            switch (type) {
                //显示添加
                case 0:
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_add_btn2, parent, false);
                    holder = new ViewHolder(convertView);
                    convertView.setTag(holder);
                    break;
                //显示内容
                case 1:
                    convertView = LayoutInflater.from(context).inflate(R.layout.bank_listview_item, parent, false);
                    holder1 = new ViewHolder1(convertView);
                    convertView.setTag(holder1);
                    break;
            }
        } else {
            switch (type) {
                //显示添加
                case 0:
                    holder = (ViewHolder) convertView.getTag();
                    break;
                //显示内容
                case 1:
                    holder1 = (ViewHolder1) convertView.getTag();
                    break;
            }
        }
        switch (type) {
            //显示添加
            case 0:
                convertView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(context, BindBankActivity.class);
                        context.startActivity(intent);
                    }
                });
                break;
            //显示内容
            case 1:
                /*if (i == start) {
                    convertView.setBackgroundResource(R.drawable.bank_item1);
                }
                if (i == close) {
                    convertView.setBackgroundResource(R.drawable.bank_item);
                }
                if (all) {
                    for (int j = 0; j < list.size(); j++) {
                        convertView.setBackgroundResource(R.drawable.bank_item);
                    }
                }*/
                String headImg = list.get(i).getImg();
                if (!"".equals(headImg)) {
                    Glide.with(context).load(OKUtilAPI.imgBaseUrl + headImg).into(holder1.bankImg);
                }
                //Glide.with(context).load("http://www.taopic.com/uploads/allimg/120421/107063-12042114025737.jpg").into(holder1.bankImg);
                holder1.bankType.setText(list.get(i).getCardType());
                String a = list.get(i).getBankAccount();
                String b = a.substring(a.length() - 4, a.length());
                holder1.bankAccount.setText("*** **** " + b);

                /*String backImg = list.get(i).getBackground();
                if (!"".equals(headImg)) {
                    final ViewHolder1 finalHolder = holder1;
                    Glide.with(context).load(OKUtilAPI.imgBaseUrl + backImg).into(new SimpleTarget<GlideDrawable>() {
                        @Override
                        public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                            finalHolder.layoutBank.setBackground(resource);
                        }
                    });
                }*/

               // int width = (int)(TDevice.getScreenWidth()-110)/2;
                //横屏
                int height;
                if (orientation==0||orientation==2){
                    height = (int)(TDevice.getScreenHeight()/7);
                }else {
                    height = (int)(TDevice.getScreenHeight()/6.5);
                }
                //holder1.layoutBank.getLayoutParams().width=width;
                holder1.layoutBank.getLayoutParams().height=height;
                final ViewHolder1 finalHolder = holder1;
                //http://img5.imgtn.bdimg.com/it/u=2299598487,3695074602&fm=21&gp=0.jpg
                Glide.with(context).load("http://www.taopic.com/uploads/allimg/120421/107063-12042114025737.jpg").into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        //finalHolder.layoutBank.setLayoutParams(new AutoLinearLayout.LayoutParams(1242,200));
                        finalHolder.layoutBank.setBackground(resource);
                    }
                });
                break;
        }
        return convertView;
    }

    class ViewHolder1 {
        @BindView(R.id.bank_img)
        ImageView bankImg;
        @BindView(R.id.bank_type)
        TextView bankType;
        @BindView(R.id.bank_chuxu)
        TextView bankChuxu;
        @BindView(R.id.bank_account)
        TextView bankAccount;
        @BindView(R.id.bank_background)
        AutoLinearLayout layoutBank;
        ViewHolder1(View view) {
            ButterKnife.bind(this, view);
        }
    }


    static class ViewHolder {
        @BindView(R.id.add_bank)
        TextView addBank;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
